/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file main.h
 * @author Nations
 * @version v1.0.1
 *
 * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 */
#ifndef __MAIN_H__
#define __MAIN_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>
#include <stdint.h>
#include "user_motor_drv.h"
#include "gd32l23x.h"
#include "log.h"



#define _D_ __DATE__
#define _T_ __TIME__


#ifdef DEBUG_VERSION

#define FW_VERSION  (_D_""_T_)      //软件版本

#else

#define FW_VERSION "FW:3.0.0"

#endif


//#define LINURM_DEBUG_WILL_DELETE


#define SERVICE_UUID                (ATT_SVC_UKEY_SERVICE)
#define USER_WRITE_NOTIFY_CHAR      (ATT_CHAR_WRITE_NOTIFY)

#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif

typedef enum {
    BT_IDLE = 0,
    BT_INITIALIZED,
    BT_ADVERTISING,
    BT_CONNECTED,
    BT_DISCONNECTED,
    BT_STS_TOTAL
} BT_SERVER_STS;
extern BT_SERVER_STS gBT_STS;


void delay_1ms(uint32_t t);
uint8_t mainboard_enter_stop2(void);
void mainboard_exit_stop2(void);
void SetSysClock_HSE_PLL(uint32_t pllmul);
void SetSysClock_HSI(void);
void hlt_rtc_init(void);
void IWDG_ReloadKey_hook(uint16_t line);
#define IWDG_ReloadKey() IWDG_ReloadKey_hook(__LINE__)
void power_off_hook(uint16_t line);
#define power_off() power_off_hook(__LINE__)
void hw_power_on(void);
void hw_power_off(void);
ErrStatus trng_ready_check(void);

//#define LINURM_DEBUG_BUZZER_OFF

//#define LINURM_DEBUG_BOOT_LOG_FLASH



//extern uint8_t bt_data_buf[64];
//extern uint8_t pvd_happend;

//#define SEND_BLE //send_to_ble(bt_data_buf,__LINE__)
//#/define SEND_ROTATE_TEST_DATA //s/end_test_rotate_ble(bt_data_buf,__LINE__)
//#define SEND_BUG_DATA //send_ble_bug(bt_data_buf,__LINE__)

#define START_COUNT 2
#define COUNT_LEN 1

//#define BUTTOM_GAER_NUM 180
//#define ROTATE_GAER_NUM 35

//#define BUTTOM_GAER_NUM 90
//#define ROTATE_GAER_NUM 53

#define BUTTOM_GAER_NUM 180
#define ROTATE_GAER_NUM 35


#define CHECK_TIMES 5
#define END_LUN 20
#define MAX_LUN 999

#define     POWER_ON     do{        \
                gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_11);  \
                gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);\
                gpio_bit_set(GPIOA, GPIO_PIN_11);   \
            }while(0)
#define     POWER_OFF     do{       \
                    gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_11);    \
                    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);\
                    gpio_bit_reset(GPIOA, GPIO_PIN_11); \
                }while(0)

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H__ */
